﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace Mosdar_Weld
{
    class Encrypt
    {

        //设置AES加密解密参数
        private RijndaelManaged Setting()
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes("TestTestTestTest"), //加密密钥,自己设置，长度必须为16字节的倍数
                IV = Encoding.UTF8.GetBytes("1234567812345678"),  //加密的iv偏移量,长度必须为16字节的倍数
                Mode = CipherMode.CBC,       //加密模式，ECB、CBC、CFB等
                Padding = PaddingMode.PKCS7, //待加密的明文长度不满足条件时使用的填充模式，PKCS7是python中默认的填充模式
                BlockSize = 128              //加密操作的块大小
            };
            return rijndaelCipher;
        }

        //获取本地文件路径
        private string GetFilePath()
        {
            string filePath = string.Empty;
            try
            {
                //选择本地文件
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "所有文件|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                    filePath = openFileDialog.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return filePath;
        }

        //读取文件内容
        private byte[] FileReadBytesToEnd(string fp)
        {
            try
            {
                FileStream fs = new FileStream(fp, FileMode.Open, FileAccess.Read);
                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, buf.Length);
                if (fs != null)
                    fs.Close();
                return buf;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return new byte[0];
        }


        //文件加密
        public void EncryptFile()
        {
            try
            {
                //选择本地文件
                string filePath = GetFilePath();
                if (filePath == string.Empty)
                    return;

                //读取文件内容
                byte[] fileData = FileReadBytesToEnd(filePath);
                if (fileData.Length == 0)
                    return;

                //设定加密参数
                RijndaelManaged rijndaelCipher = Setting();

                //加密文件内容
                ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); //创建加密对象
                byte[] cipherBytes = transform.TransformFinalBlock(fileData, 0, fileData.Length);

                //将加密后的文件内容写入原来的文件
                string contentStr = Convert.ToBase64String(cipherBytes); //将字节数组转为base64字符串保存，防止乱码
                File.WriteAllText(filePath, contentStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        //文件解密
        public void DecryptFile()
        {
            try
            {
                //选择本地文件
                string filePath = GetFilePath();
                if (filePath == string.Empty)
                    return;

                //读取文件内容
                byte[] fileData = FileReadBytesToEnd(filePath);
                if (fileData.Length == 0)
                    return;

                //由于文件内容是base64格式编码的字符串，所以需要先进行base64解码
                string decryptStr = Encoding.UTF8.GetString(fileData);
                fileData = Convert.FromBase64String(decryptStr);

                //设定解密参数，与加密参数保持一致
                RijndaelManaged rijndaelCipher = Setting();

                //解密文件内容
                ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); // 创建解密对象
                byte[] cipherBytes = transform.TransformFinalBlock(fileData, 0, fileData.Length);

                //解密后的文件内容写入原来的文件
                string contentStr = Encoding.UTF8.GetString(cipherBytes); //将字节数组转为字符串保存
                File.WriteAllText(filePath, contentStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
